package org.lbm.flow.builder;

import org.lbm.flow.agent.Agent;
import org.lbm.flow.engine.FlowEngine;
import org.lbm.flow.node.AgentNode;
import org.lbm.flow.node.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * 工作流构建器
 *
 * @author insist
 * @since 2025-08-01
 * 提供流式API来构建工作流
 */
public class FlowBuilder {

    private final String flowId;
    private final List<Node> nodes;
    private final List<Connection> connections;

    public FlowBuilder(String flowId) {
        this.flowId = flowId;
        this.nodes = new ArrayList<>();
        this.connections = new ArrayList<>();
    }

    /**
     * 添加智能体节点
     */
    public FlowBuilder addAgentNode(String nodeId, String name, String description, Agent agent) {
        AgentNode node = new AgentNode(nodeId, name, description, agent);
        nodes.add(node);
        return this;
    }

    /**
     * 添加智能体节点（使用简单智能体）
     */
    public FlowBuilder addSimpleAgentNode(String nodeId, String name, String description) {
        org.lbm.flow.agent.impl.SimpleAgent agent =
            new org.lbm.flow.agent.impl.SimpleAgent(nodeId, name, description);
        return addAgentNode(nodeId, name, description, agent);
    }

    /**
     * 添加智能体节点（使用简单智能体，指定行为）
     */
    public FlowBuilder addSimpleAgentNode(String nodeId, String name, String description,
                                        org.lbm.flow.agent.impl.SimpleAgent.AgentBehavior behavior) {
        org.lbm.flow.agent.impl.SimpleAgent agent =
            new org.lbm.flow.agent.impl.SimpleAgent(nodeId, name, description, behavior);
        return addAgentNode(nodeId, name, description, agent);
    }

    /**
     * 添加自定义节点
     */
    public FlowBuilder addNode(Node node) {
        nodes.add(node);
        return this;
    }

    /**
     * 连接两个节点
     */
    public FlowBuilder connect(String fromNodeId, String toNodeId) {
        connections.add(new Connection(fromNodeId, toNodeId));
        return this;
    }

    /**
     * 连接多个节点（链式连接）
     */
    public FlowBuilder chain(String... nodeIds) {
        if (nodeIds.length < 2) {
            throw new IllegalArgumentException("链式连接至少需要2个节点");
        }

        for (int i = 0; i < nodeIds.length - 1; i++) {
            connect(nodeIds[i], nodeIds[i + 1]);
        }

        return this;
    }

    /**
     * 并行连接（从同一个节点连接到多个目标节点）
     */
    public FlowBuilder parallel(String fromNodeId, String... toNodeIds) {
        for (String toNodeId : toNodeIds) {
            connect(fromNodeId, toNodeId);
        }
        return this;
    }

    /**
     * 汇聚连接（从多个源节点连接到同一个目标节点）
     */
    public FlowBuilder converge(String[] fromNodeIds, String toNodeId) {
        for (String fromNodeId : fromNodeIds) {
            connect(fromNodeId, toNodeId);
        }
        return this;
    }

    /**
     * 构建工作流引擎
     */
    public FlowEngine build() {
        FlowEngine engine = new FlowEngine(flowId);

        // 添加所有节点
        for (Node node : nodes) {
            engine.addNode(node);
        }

        // 建立连接
        for (Connection connection : connections) {
            engine.connect(connection.fromNodeId, connection.toNodeId);
        }

        return engine;
    }

    /**
     * 构建并执行工作流
     */
    public org.lbm.flow.engine.FlowResult buildAndExecute() {
        FlowEngine engine = build();
        try {
            return engine.execute();
        } finally {
            engine.shutdown();
        }
    }

    /**
     * 获取节点列表
     */
    public List<Node> getNodes() {
        return new ArrayList<>(nodes);
    }

    /**
     * 获取连接列表
     */
    public List<Connection> getConnections() {
        return new ArrayList<>(connections);
    }

    /**
     * 连接信息
     */
    public static class Connection {
        public final String fromNodeId;
        public final String toNodeId;

        public Connection(String fromNodeId, String toNodeId) {
            this.fromNodeId = fromNodeId;
            this.toNodeId = toNodeId;
        }

        @Override
        public String toString() {
            return fromNodeId + " -> " + toNodeId;
        }
    }
}
